<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Backyard extends CI_Controller
{
	private $user_id;
	function __construct()
	{
		parent::__construct();
        $this->load->config('layout', TRUE);
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('security');
		$this->load->library('tank_auth');
		$this->load->library('backyard_view');
		$this->load->model('post_model');
		$this->load->model('topic_model');
		$this->lang->load('tank_auth');
		$this->lang->load('backyard');
		$this->user_id = $this->session->userdata('user_id');
	}

	function index()
	{
		if ($this->tank_auth->is_logged_in()) {									// logged in
			redirect('/backyard/dashboard/');

		} elseif ($this->tank_auth->is_logged_in(FALSE)) {						// logged in, not activated
			redirect('/backyard_auth/send_again/');

		}
		else {																	// not logged in
			redirect('/backyard_auth/login/');
		}
	}

	function dashboard()
	{
		if ($this->tank_auth->is_logged_in()) {									// logged in
			$data = '';
			$data['all_post'] = $this->post_model->get_recent_post();
			$data['all_topic'] = $this->topic_model->get_recent_topic();
			$data['recent_post'] = $this->load->view('backyard/recent_post', $data,true);
			$data['recent_topic'] = $this->load->view('backyard/recent_topic', $data,true);
			$this->load->view('backyard/index', $data);

		} elseif ($this->tank_auth->is_logged_in(FALSE)) {						// logged in, not activated
			redirect('/backyard_auth/send_again/');

		}
		else {																	// not logged in
			redirect('/backyard_auth/login/');
		}
	}

	function all_post(){ // get selfown post
		if ($this->tank_auth->is_logged_in()) {									// logged in
			$data['message'] = $this->session->flashdata('action_msg');
			$data['post_type'] = 'all_post';
			$data['all_post'] = $this->post_model->get_all_post_by_user_id($this->user_id);
			$data['table_content'] = $this->load->view('backyard/all_post_table_content', $data,true);
			$this->load->view('backyard/all_post', $data);

		} elseif ($this->tank_auth->is_logged_in(FALSE)) {						// logged in, not activated
			redirect('/backyard_auth/send_again/');

		}
		else {																	// not logged in
			redirect('/backyard_auth/login/');
		}

	}

	function all_topic(){
		if ($this->tank_auth->is_logged_in()) {									// logged in
			$data['message'] = $this->session->flashdata('action_msg');
			$data['post_type'] = 'all_topic';
			$data['all_topic'] = $this->topic_model->get_all_topic_by_user_id($this->user_id);
			$data['table_content'] = $this->load->view('backyard/all_topic_table_content', $data,true);
			$this->load->view('backyard/all_topic', $data);

		} elseif ($this->tank_auth->is_logged_in(FALSE)) {						// logged in, not activated
			redirect('/backyard_auth/send_again/');

		}
		else {																	// not logged in
			redirect('/backyard_auth/login/');
		}
	}

	function all_share(){
		if ($this->tank_auth->is_logged_in()) {									// logged in
			$data['message'] = $this->session->flashdata('action_msg');
			$data['post_type'] = 'all_post';
			$data['all_share'] = $this->post_model->get_all_share_unproved();
			//echo "<pre>";print_r($data);echo "</pre>";exit;
			$data['table_content'] = $this->load->view('backyard/all_share_table_content', $data,true);
			$this->load->view('backyard/all_share', $data);

		} elseif ($this->tank_auth->is_logged_in(FALSE)) {						// logged in, not activated
			redirect('/backyard_auth/send_again/');

		}
		else {																	// not logged in
			redirect('/backyard_auth/login/');
		}
	}

	function post_new(){
		if ($this->tank_auth->is_logged_in()) {									// logged in
			$data['post_type'] = 'post_new';
			$data['category'] = $this->config->item('category', 'layout');
            $data['image_uploader'] = $this->load->view('backyard/image_uploader', $data,true);
			$this->load->view('backyard/post_new', $data);

		} elseif ($this->tank_auth->is_logged_in(FALSE)) {						// logged in, not activated
			redirect('/backyard_auth/send_again/');

		}
		else {																	// not logged in
			redirect('/backyard_auth/login/');
		}
	}

	function topic_new(){
		if ($this->tank_auth->is_logged_in()) {									// logged in
			$data['post_type'] = 'topic_new';
			$data['category'] = $this->config->item('topic_category', 'layout');
            $data['image_uploader'] = $this->load->view('backyard/image_uploader', $data,true);
			$this->load->view('backyard/topic_new', $data);

		} elseif ($this->tank_auth->is_logged_in(FALSE)) {						// logged in, not activated
			redirect('/backyard_auth/send_again/');

		}
		else {																	// not logged in
			redirect('/backyard_auth/login/');
		}
	}
    
    function post_modify(){
    	$post_id = $this->uri->segment(3);
    	$data['user_id'] = $this->user_id;
    	if(is_null($post_id)){ //not assign post id
    		redirect('/'); 
    	}

		if ($this->tank_auth->is_logged_in()) {									// logged in
			$data['post_type'] = 'post_new'; // same as post new type
			$data['post'] = $this->post_model->get_post_by_id($post_id);
			$data['category'] = $this->config->item('category', 'layout');
			$data['upload_image'] = json_decode($data['post'][0]['upload_image']);
            $data['image_uploader'] = $this->load->view('backyard/image_uploader', $data,true);
            $data['post_id'] = $post_id;
			$this->load->view('backyard/post_modify', $data);

		} elseif ($this->tank_auth->is_logged_in(FALSE)) {						// logged in, not activated
			redirect('/backyard_auth/send_again/');

		}
		else {																	// not logged in
			redirect('/backyard_auth/login/');
		}
	}

	function topic_modify(){
    	$topic_id = $this->uri->segment(3);
    	$data['user_id'] = $this->user_id;
    	if(is_null($topic_id)){ //not assign topic id
    		redirect('/'); 
    	}

		if ($this->tank_auth->is_logged_in()) {									// logged in
			$data['post_type'] = 'topic_new'; // same as post new type
			$data['topic'] = $this->topic_model->get_topic_by_id($topic_id);
			$data['category'] = $this->config->item('topic_category', 'layout');
			$data['upload_image'] = json_decode($data['topic'][0]['upload_image']);
            $data['image_uploader'] = $this->load->view('backyard/image_uploader', $data,true);
            $data['topic_id'] = $topic_id;
			$this->load->view('backyard/topic_modify', $data);

		} elseif ($this->tank_auth->is_logged_in(FALSE)) {						// logged in, not activated
			redirect('/backyard_auth/send_again/');

		}
		else {																	// not logged in
			redirect('/backyard_auth/login/');
		}
	}

	function changeHomeViewStatus(){
		$data = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
		
		$update_status = $this->post_model->update_home_view_status($data['share_id'],$data['status']);
		$return = array('update_status'=>$update_status);
		echo json_encode($return);
	}

	function acceptIdea(){
		$data = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
		$this->post_model->accept_idea($data['idea_id'],$data['post_tags']);
		$this->_send_email('accept_idea',$data['email'],$data);

		$return = array();
		$return['msg'] = '審核通過，已發信通知使用者';
		echo json_encode($return);

	}

	function show_reject_form(){
		$data = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
		$return = array();
		$fe_post = $this->post_model->get_fe_post_by_id($data['idea_id']);
		$data['email'] = $fe_post[0]['email'];
		$data['idea_title'] = $fe_post[0]['post_excerpt'];
		$return['html'] = $this->load->view('backyard/reject_idea_form', $data ,true);
		echo json_encode($return);
	}

	function show_accept_form(){
		$data = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
		$return = array();
		$fe_post = $this->post_model->get_fe_post_by_id($data['idea_id']);
		$data['email'] = $fe_post[0]['email'];
		$data['idea_title'] = $fe_post[0]['post_excerpt'];
		$return['html'] = $this->load->view('backyard/accept_idea_form', $data ,true);
		echo json_encode($return);
	}

	function rejectIdea(){
		$data = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
		$reject_reason = json_encode(array('reject_title'=>$data['reject_title'],'reject_reason'=>$data['reject_reason']));
		$this->post_model->reject_idea($data['idea_id'],$reject_reason);
		$this->_send_email('reject_idea',$data['email'],$data);

		$return = array();
		$return['msg'] = '已發信通知使用者';
		echo json_encode($return);
	}

	/**
	 * Send email message to frontend user
	 *
	 * @param	string
	 * @param	string
	 * @param	array
	 * @return	void
	 */
	function _send_email($type, $email, &$data)
	{
		$data['site_name'] = $this->config->item('website_name', 'tank_auth');
		$this->load->library('email');
		$this->email->from($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth'));
		$this->email->reply_to($this->config->item('webmaster_email', 'tank_auth'), $this->config->item('website_name', 'tank_auth'));
		$this->email->to($email);
		$this->email->subject(sprintf($this->lang->line('auth_subject_'.$type), $this->config->item('website_name', 'tank_auth')));
		$this->email->message($this->load->view('email/'.$type.'-html', $data, TRUE));
		$this->email->set_alt_message($this->load->view('email/'.$type.'-txt', $data, TRUE));
		$this->email->send();
	}
}

/* End of file backyard.php */
/* Location: ./application/controllers/backyard.php */